package com.pzhu.user.controller.manage;

import com.pzhu.base.common.utils.result.PageData;
import com.pzhu.base.common.utils.result.Result;
import com.pzhu.base.common.utils.result.Results;
import com.pzhu.user.domain.ro.RoleCreateRO;
import com.pzhu.user.domain.ro.RoleSearchRO;
import com.pzhu.user.domain.ro.RoleUpdateRO;
import com.pzhu.user.domain.vo.RoleSearchVO;
import com.pzhu.user.service.impl.RoleService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.RoleInfo;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/api/manage/roles")
@RequiredArgsConstructor
@Validated
public class RoleManageController {

    public final RoleService roleService;

    /**
     * 列表
     *
     * @param roleSearchRo 角色搜索需求
     * @return {@link Result }<{@link Collection }<{@link RoleSearchVO }>>
     */
    @GetMapping
    public Result<Collection<RoleSearchVO>> list(RoleSearchRO roleSearchRo) {
        PageData<RoleSearchVO> result = roleService.search(roleSearchRo);
        return Results.page(result);
    }

    /**
     * 信息
     *
     * @param roleId 角色ID
     * @return {@link Result }<{@link RoleInfo }>
     */
    @GetMapping("/{roleId}")
    public Result<RoleSearchVO> info(@PathVariable Long roleId) {
        RoleSearchVO roleSearchVo = roleService.info(roleId);
        return Results.success(roleSearchVo);
    }

    /**
     * 创造
     *
     * @param roleCreateRo 角色创建需求
     * @return {@link Result }<{@link Long }>
     */
    @PostMapping
    public Result<Long> create(@Valid @RequestBody RoleCreateRO roleCreateRo) {
        Long roleId = roleService.save(roleCreateRo);
        return Results.success(roleId);
    }

    /**
     * 更新
     *
     * @param roleId 角色ID
     * @param roleUpdateRo 角色更新要求
     * @return {@link Result }<{@link Boolean }>
     */
    @PutMapping("/{roleId}")
    public Result<Boolean> update(@PathVariable Long roleId, @Valid @RequestBody RoleUpdateRO roleUpdateRo) {
        boolean result = roleService.update(roleId, roleUpdateRo);
        return Results.success(result);
    }

    /**
     * 删除
     *
     * @param roleId 角色ID
     * @return {@link Result }<{@link Boolean }>
     */
    @DeleteMapping("/{roleId}")
    public Result<Boolean> delete(@PathVariable Long roleId) {
        boolean result  = roleService.removeById(roleId);
        return Results.success(result);
    }

    /**
     * 批量删除
     *
     * @param roleIds 角色ID
     * @return {@link Result }<{@link Boolean }>
     */
    @DeleteMapping("batch")
    public Result<Boolean> batchDelete(@NotBlank(message = "删除角色id不能为空！") String roleIds) {
        List<Long> roleList = Arrays.stream(roleIds.split(",")).map(Long::valueOf).toList();
        boolean result = roleService.removeByIds(roleList);
        return Results.success(result);
    }
}
